//
//  Stack.cpp
//  博客代码
//
//  Created by 马鹏朔 on 2022/11/17.
//

#include "Stack.hpp"
void StackInit(ST*ps)
{
    assert(ps);
    ps->a=NULL;
    ps->top=ps->capicity=0;
}
void StackDestroy(ST*ps)
{
    assert(ps);
    free(ps);
    ps=NULL;
    ps->top=0;
    ps->capicity=0;
}
void StackPush(ST*ps,STDataType x)
{
    assert(ps);
    
   
    if(ps->top==ps->capicity)
    {
        int  newcapicity=ps->capicity==0?4:(ps->capicity)*2;
        //newcapicity==0?4:newcapicity*2;
        STDataType* ptr = (STDataType*)realloc(ps->a, newcapicity*sizeof(STDataType));
        if(ptr==NULL)
        {
            perror("StackPush");
            exit(-1);
        }
        ps->a=ptr;
        ps->capicity=newcapicity;
    }
    ps->a[ps->top]=x;
    ++ps->top;
}
bool StackEmpty(ST*ps)
{
    assert(ps);
    return ps->top==0;
}
void StackPop(ST*ps)
{
    assert(ps);
    assert(!StackEmpty(ps));
    --ps->top;
}
STDataType StackTop(ST* ps)
{
    assert(ps);
    assert(!StackEmpty(ps));
    
    return ps->a[ps->top - 1];
}
int StackSize(ST* ps)
{
    assert(ps);

    return ps->top;
}
